#include "bsp_timer.h"


void Timer_Init(BSP_Timer_TypeDef* t, bool ISR_Need)
{
    t->RCC_APBxPeriphClockCmd(t->RCC_APBxPeriph_TIMy, ENABLE);

    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_TimeBaseInitStruct.TIM_ClockDivision = t->TIM_ClockDivision;
    TIM_TimeBaseInitStruct.TIM_CounterMode = t->TIM_CounterMode;
    TIM_TimeBaseInitStruct.TIM_Period = t->TIM_Period;
    TIM_TimeBaseInitStruct.TIM_Prescaler = t->TIM_Prescaler;
    TIM_TimeBaseInitStruct.TIM_RepetitionCounter = t->TIM_RepetitionCounter;
    TIM_TimeBaseInit(t->TIMx, &TIM_TimeBaseInitStruct);

    if (ISR_Need) {
        TIM_ClearFlag(t->TIMx, t->TIM_FLAG);
        TIM_ITConfig(t->TIMx, t->TIM_IT, ENABLE);

        NVIC_PriorityGroupConfig(t->NVIC_PriorityGroupx);

        NVIC_InitTypeDef NVIC_InitStruct;
        NVIC_InitStruct.NVIC_IRQChannel = t->NVIC_IRQChannel;
        NVIC_InitStruct.NVIC_IRQChannelCmd = t->NVIC_IRQChannelCmd;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = t->NVIC_IRQChannelPreemptionPriority;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority = t->NVIC_IRQChannelSubPriority;
        NVIC_Init(&NVIC_InitStruct);
    }

    TIM_Cmd(t->TIMx, ENABLE);
}



